﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Linq
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class OverrideAttribute : Attribute
    {
        public static readonly string WildCard = "*";

        public OverrideAttribute(string propertyName)
            : this(propertyName, null)
        {

        }

        public OverrideAttribute(string propertyName, string condition)
        {
            if (string.IsNullOrWhiteSpace(propertyName))
                throw new ArgumentNullException("propertyName");

            _propertyName = propertyName;
            _condition = condition;
        }

        private string _propertyName;

        public string PropertyName
        {
            get { return _propertyName; }
        }

        private string _condition;

        public string Condition
        {
            get { return _condition; }
        }


        internal bool ShouldOverride(QueryArgs instance)
        {
            if (!string.IsNullOrWhiteSpace(_condition))
            {
                return (bool)System.Linq.Dynamic.DynamicExpression
                    .ParseLambda(instance.GetType(), typeof(bool), _condition)
                    .Compile().DynamicInvoke(instance);
            }
            else
                return true;
        }
    }
}
